package com.cskaoyan.service;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.brandbo.BrandCreateBO;
import com.cskaoyan.bean.brandbo.BrandListBO;
import com.cskaoyan.bean.brandbo.BrandUpdateBO;
import com.cskaoyan.bean.brandvo.BrandListVO;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/05/06 16:01
 *
 * @author shuyang
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public BrandListVO selectBrands(BrandListBO brandListBO) {
        Integer page = brandListBO.getPage();//页码
        Integer limit = brandListBO.getLimit();//数据量
        String sort = brandListBO.getSort();//排序列
        String order = brandListBO.getOrder();//desc\asc
        String name = brandListBO.getName();
        Integer id = brandListBO.getId();
        PageHelper.startPage(page, limit);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && (!("".equals(name)))) {
            name = "%" + name + "%";
            criteria.andNameLike(name);
        }
        marketBrandExample.setOrderByClause(sort + " " + order);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        int total = (int) marketBrandPageInfo.getTotal();
        int pages = marketBrandPageInfo.getPages();
        return new BrandListVO(total, pages, limit, page, marketBrands);
    }

    @Override
    public MarketBrand insertBrand(BrandCreateBO brandCreateBO) {
        MarketBrand marketBrand1 = new MarketBrand();
        marketBrand1.setDesc(brandCreateBO.getDesc());
        marketBrand1.setFloorPrice(new BigDecimal(brandCreateBO.getFloorPrice()));
        marketBrand1.setName(brandCreateBO.getName());
        marketBrand1.setPicUrl(brandCreateBO.getPicUrl());
        marketBrand1.setAddTime(new Date());
        marketBrand1.setUpdateTime(new Date());
        marketBrandMapper.insertSelective(marketBrand1);
        MarketBrand marketBrand2 = marketBrandMapper.selectByPrimaryKey(marketBrand1.getId());
        return marketBrand2;
    }

    @Override
    public MarketBrand UpdateBrand(BrandUpdateBO brandUpdateBO) {
        MarketBrand marketBrand1 = new MarketBrand();
        marketBrand1.setDesc(brandUpdateBO.getDesc());
        marketBrand1.setFloorPrice(new BigDecimal(brandUpdateBO.getFloorPrice()));
        marketBrand1.setName(brandUpdateBO.getName());
        marketBrand1.setPicUrl(brandUpdateBO.getPicUrl());
        marketBrand1.setUpdateTime(new Date());
        marketBrand1.setId(brandUpdateBO.getId());
        marketBrand1.setSortOrder(brandUpdateBO.getSortOrder());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andIdEqualTo(brandUpdateBO.getId());
        marketBrandMapper.updateByExampleSelective(marketBrand1,marketBrandExample);
        MarketBrand marketBrand2 = marketBrandMapper.selectByPrimaryKey(brandUpdateBO.getId());
        return marketBrand2;
    }

    @Override
    public void deleteBrand(MarketBrand marketBrand) {
        //1表示已逻辑删除
        int status = 1;
        marketBrandMapper.updateStatusById(marketBrand.getId(),status);
    }

}
